package base.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*视频处理类*/

public class FFmpegUtil {

	
	 /** 
     * 获取视频总时间 
     * @param viedo_path    视频路径 
     * @param ffmpeg_path   ffmpeg路径 
     * @return 
     */  
    public static int getVideoTime(String video_path) {  
       
        try { 
        	
        	//video_path = "/usr/local/tomcat/movie/a.mp4";
        	String command = "ffmpeg -i " + video_path;
        	
        	Runtime rt = Runtime.getRuntime();
			Process proc = rt.exec(command);
			InputStream stderr = proc.getErrorStream();
			InputStreamReader isr = new InputStreamReader(stderr);
			BufferedReader br = new BufferedReader(isr);
			String line = null;
			String str = null;
			while ((line = br.readLine()) != null){
				str+=line;
			}
        	
        	
            //从视频信息中解析时长  
            String regexDuration = "Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s";  
            Pattern pattern = Pattern.compile(regexDuration);  
            Matcher m = pattern.matcher(str);  
            if (m.find()) {  
                int time = getTimelen(m.group(1));
                
                //log.info(video_path+",视频时长："+time+", 开始时间："+m.group(2)+",比特率："+m.group(3)+"kb/s");
               

                System.out.println(video_path+",movieTotal:"+time+", movieStart:"+m.group(2)+",movieBit:"+m.group(3)+"kb/s");
                return time;  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
          
        return 0;  
    }  
      
    //格式:"00:00:10.68"  
    private static int getTimelen(String timelen){  
        int min=0;  
        String strs[] = timelen.split(":");  
        if (strs[0].compareTo("0") > 0) {  
            min+=Integer.valueOf(strs[0])*60*60;//秒  
        }  
        if(strs[1].compareTo("0")>0){  
            min+=Integer.valueOf(strs[1])*60;  
        }  
        if(strs[2].compareTo("0")>0){  
            min+=Math.round(Float.valueOf(strs[2]));  
        }  
        return min;  
    }  

    
 // 视频缩略图截取
 	// inFile 输入文件(包括完整路径)
 	// outFile 输出文件(可包括完整路径)
 	public static boolean transfer2(String inFile, String outFile, int time) {

 		// ffmpeg -i a.mp4 -y -f image2 -ss 8 -t 0.001 -s 350x240 test3.jpg

 		//inFile = "/usr/local/tomcat/movie/a.mp4";
 		//outFile = "/usr/local/tomcat/movie/abcde.jpg";"ffmpeg -i /usr/local/tomcat/webapps/mommy/file/98b3e5d3-0bcc-491b-a238-6f72e0b839f2.mp4 -y -f image2 -ss 00:00:10 -t 00:00:01 -s 176x144 /usr/local/tomcat/webapps/mommy/file/2.jpg"

 		String command = "ffmpeg -i " + inFile+ " -y -f image2 -ss "+time+" -t 0.001 -s 780x570 "
 				+ outFile;
 		try { 
 			Runtime rt = Runtime.getRuntime();
 			Process proc = rt.exec(command);
 			InputStream stderr = proc.getErrorStream();
 			InputStreamReader isr = new InputStreamReader(stderr);
 			BufferedReader br = new BufferedReader(isr);
 			String line = null;
 			while ((line = br.readLine()) != null)
 				System.out.println(line);

 		} catch (Throwable t) {
 			t.printStackTrace();
 			return false;
 		}

 		return true;

 	}
    
	// 视频缩略图截取
	// inFile 输入文件(包括完整路径)
	// outFile 输出文件(可包括完整路径)
	public static boolean transfer(String inFile, String outFile) {

		// ffmpeg -i a.mp4 -y -f image2 -ss 8 -t 0.001 -s 350x240 test3.jpg

		//inFile = "/usr/local/tomcat/movie/a.mp4";
		//outFile = "/usr/local/tomcat/movie/abcde.jpg";"ffmpeg -i /usr/local/tomcat/webapps/mommy/file/98b3e5d3-0bcc-491b-a238-6f72e0b839f2.mp4 -y -f image2 -ss 00:00:10 -t 00:00:01 -s 176x144 /usr/local/tomcat/webapps/mommy/file/2.jpg"

		String command = "ffmpeg -i " + inFile+ " -y -f image2 -ss 1 -t 0.001 -s 780x570 "
				+ outFile;
		try { 
			Runtime rt = Runtime.getRuntime();
			Process proc = rt.exec(command);
			InputStream stderr = proc.getErrorStream();
			InputStreamReader isr = new InputStreamReader(stderr);
			BufferedReader br = new BufferedReader(isr);
			String line = null;
			while ((line = br.readLine()) != null)
				System.out.println(line);

		} catch (Throwable t) {
			t.printStackTrace();
			return false;
		}

		return true;

	}

	
}
